<template>
  <div class="login-container" :style="loginBg">
    <h3>万店互联</h3>
    <p class="title">{{ $t('login.title') }}</p>
    <!-- <div :style="loginLogo" class="loginLogo"></div>
          <div class="title-container">
          </div> -->
    <el-tabs v-model="activeName" stretch class="login-form" style="padding:5px 0" type="border-card">

      <el-tab-pane label="验证码登录" name="first">

        <!-- 手机号 -->
        <p class="title-p" style="margin-top:26px"> 手机号</p>
        <!-- <el-form-item prop="username"> -->
        <InputGroup
          v-model="phone"
          placeholder="请输入手机号"
          :error="errors.phone"
          @blur="validatePhone"
        />
        <!-- </el-form-item> -->
        <p class="title-p" style="margin-top:26px">验证码</p>
        <!-- <el-form-item prop="password"> -->
        <!-- 输入验证码 -->

        <InputGroup
          v-model="verifyCode"
          placeholder="请输入验证码"
          :btn-title="btnTitle"
          :disabled="disabled"
          :error="errors.code"
          @btnClick="getVerifyCode"
          @keyup.enter.native="handleLogin"
        />
        <!-- </el-form-item> -->
        <!-- 登录按钮 -->

        <el-button
          :loading="loading"
          type="primary"
          style="width: 164px;height: 44px;margin: 22px auto;display: table;"
          @click="handleLogin"
        >立即登录</el-button>
      <!-- </el-form> -->
      </el-tab-pane>
      <el-tab-pane label="账号登录" name="second">
        <el-form
          v-if="activeName=='second'"
          ref="loginForm"
          :model="loginForm"
          :rules="loginRules"
          class="login-form"
          auto-complete="on"
          label-position="left"
        >
          <!-- <div :style="loginLogo" class="loginLogo"></div>
          <div class="title-container">
          </div> -->
          <p class="title-p" style="margin-top:26px">手机号</p>
          <el-form-item prop="phone">
            <!-- <span class="svg-container">
              <svg-icon icon-class="user" />
            </span>-->
            <!-- :placeholder="$t('login.username')" -->
            <el-input
              v-model="loginForm.phone"
              name="phone"
              type="text"
              auto-complete="on"
              class="loginUser"
            />
          </el-form-item>
          <p class="title-p">密码</p>
          <el-form-item prop="password">

            <el-input
              v-model="loginForm.password"
              class="loginPass"
              :type="passwordType"
              name="password"
              auto-complete="on"
              @keyup.enter.native="handleLogin"
            />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>

          <el-button
            :loading="loading"
            type="primary"
            style="width: 164px;height: 44px;margin: 0 auto;display: table;"
            @click.native.prevent="handleLogin"
          >{{ $t('login.logIn') }}</el-button>
        </el-form>
      </el-tab-pane>
    </el-tabs>

    <el-dialog :title="$t('login.thirdparty')" :visible.sync="showDialog">
      {{ $t('login.thirdpartyTips') }}
      <br>
      <br>
      <br>
      <social-sign />
    </el-dialog>
  </div>
</template>

<script>
import LangSelect from '@/components/LangSelect'
import { getSms } from '@/api/login'
import SocialSign from './socialsignin'
import InputGroup from '@/components/Logininput'

export default {
  name: 'Login',
  components: { LangSelect, SocialSign, InputGroup },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (value == '') {
        callback(new Error('手机号不能为空'))
      } else if (!/^1[3456789]\d{9}$/.test(value)) {
        callback(new Error('请输入正确的手机号'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length == '') {
        callback(new Error('密码不能为空'))
      } else {
        callback()
      }
    }
    return {
      loginBg: {
        // backgroundImage:'url('+require("../../../static/img/loginBg.png")+')',
        backgroundImage:
          'url(https://mmkapp.oss-cn-beijing.aliyuncs.com/h5_images/mmkAdmin/loginBg.png)'
      },
      // loginLogo: {
      //   backgroundImage:
      //     "url(https://mmkapp.oss-cn-beijing.aliyuncs.com/h5_images/mmkAdmin/loginLogo.png)"
      // },
      loginForm: {
        username: '',
        password: '',
        phone: '', // 手机号
        verifyCode: '' // 验证码
      },
      loginRules: {
        phone: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      passwordType: 'password',
      loading: false,
      showDialog: false,
      redirect: undefined,
      activeName: 'first',
      phone: '', // 手机号
      verifyCode: '', // 验证码
      btnTitle: '获取验证码',
      disabled: false, // 是否可点击
      errors: {} // 验证提示信息
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },

  created() {
    // window.addEventListener('hashchange', this.afterQRScan)
  },
  destroyed() {
    // window.removeEventListener('hashchange', this.afterQRScan)
  },
  methods: {

    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    handleLogin() {
      if(this.activeName=='second'){
        this.$refs.loginForm.validate(valid => {
          if (valid) {
          const data = {
              type: '2',
              phone: this.loginForm.phone,
              password: this.loginForm.password
            }
            this.$store
              .dispatch('LoginByUsername', data)
              .then(() => {
                this.$router.push({ path: this.redirect || '/' })
              })
              .catch(err => {
              })
          } else {
              console.log('error submit!!')
            return false
          }
        })
      } else{
        if (this.validatePhone() && this.validateCode()) {
              const data = {
                type: '1',
                phone: this.phone,
                code: this.verifyCode
              }
        this.$store
          .dispatch('LoginByUsername', data)
          .then(() => {
            this.$router.push({ path: this.redirect || '/' })
          })
          .catch(() => {
          })
        } else {
          return false
        }
            }
    
    },

    getVerifyCode() {
    // 获取验证码
      if (this.validatePhone()) {
        this.errors = {
          phone: null
        }
        this.validateBtn()
        // 发送网络请求
        getSms({ phone: this.phone }).then(res => {
          console.log(res)
          if (res.data.code == 200) {
            this.$message({
              message: '验证码获取成功！',
              type: 'success'
            })
          }
        })
      }
    },
    validatePhone() {
      // 判断输入的手机号是否合法
      if (!this.phone) {
        this.errors = {
          phone: '手机号码不能为空'
        }
        return false
      } else if (!/^1[3456789]\d{9}$/.test(this.phone)) {
        this.errors = {
          phone: '请输入正确是手机号'
        }
        return false
      } else {
        return true
      }
    },

    validateCode() {
      // 判断输入的手机号是否合法
      if (!this.verifyCode) {
        this.errors = {
          code: '请先填写验证码'
        }
        return false
      } else if (!/\d{6}$/.test(this.verifyCode)) {
        this.errors = {
          code: '请输入正确是验证码'
        }
        this.verifyCode = ''
        return false
      } else {
        return true
      }
    },

    validateBtn() {
    // 倒计时
      let time = 60
      const timer = setInterval(() => {
        if (time == 0) {
          clearInterval(timer)
          this.disabled = false
          this.btnTitle = '获取验证码'
        } else {
          this.btnTitle = '重新发送(' + time + ')'
          this.disabled = true
          time--
        }
      }, 1000)
    },
    handleLogins() {
      if (this.validatePhone() && this.validateCode()) {
        const data = {
          type: '1',
          phone: this.phone,
          code: this.verifyCode
        }
        this.$store
          .dispatch('LoginByUsername', data)
          .then(() => {
            // this.loading = false
            this.$router.push({ path: this.redirect || '/' })
          })
          .catch(() => {
            // this.loading = false
          })
      } else {
        //   console.log('error submit!!')
        return false
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg: white;
$light_gray: #333;
$cursor: #333;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
    &::first-line {
      color: $light_gray;
    }
  }
}

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: white;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;
      &:-webkit-autofill {
        -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    // background: rgba(0, 0, 0, 0.1);
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.loginLogo {
  height: 94px;
  width: 94px;
  background-size: contain;
  position: absolute;
  top: -47px;
  left: 193px;
}
.title-p {
  width: 80%;
  margin: 0 auto;
  font-size: 16px;
  color: #000;
  margin-bottom: 8px;
}
.login-container {
  h3{
    font-size: 42px;
    color: #eee;
    margin: 0px auto 20px auto;
    text-align: center;
    font-weight: 400;
    margin-top: 100px;
  }
  .title {
    font-size: 30px;
    color: #eee;
    margin: 0px auto 20px auto;
    text-align: center;
    font-weight: 400;
    margin-top: 30px;
  }
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  background-size: cover;
  background-position: center;
  .login-form {
    position: relative;
    width: 480px;
    max-width: 100%;
    margin: 0 auto;
    overflow: hidden;
    height: 380px;
    background: white;
    border-radius: 16px;
  }
  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }
  .title-container {
    position: relative;
    height: 4px;
    .set-language {
      color: #fff;
      position: absolute;
      top: 3px;
      font-size: 18px;
      right: 0px;
      cursor: pointer;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .thirdparty-button {
    position: absolute;
    right: 0;
    bottom: 6px;
  }

}
</style>
